package controller

import (
	"ginfast/api/weixin/dto"
	"ginfast/api/weixin/model"
	"ginfast/middleware"
	"ginfast/pkg/ctx"
	"ginfast/pkg/orm"
	"ginfast/util"

	"github.com/gin-gonic/gin"
)

// officialcfg 公众号配置参数
type officialcfg struct {
	AppID     string `json:"appId" gorm:"default:'';comment:设置微信公众号的appid"`
	AppSecret string `json:"appSecret" gorm:"default:'';comment:设置微信公众号的app secret;"`
	Token     string `json:"token" gorm:"default:'';comment:设置微信公众号的token;"`
	AesKey    string `json:"aesKey" gorm:"default:'';comment:设置微信公众号的EncodingAESKey;"`
}

type Config struct {
}

// AppGetHandler 配置
func (o *Config) AppGetHandler(c *gin.Context) {
	var (
		data   model.Config
		config dto.AppCfg
	)
	if err := orm.DbFirstBy(data, "name like ?", dto.AppCfgName); err == nil {
		util.StringVal(data.Value, &config)
	}
	ctx.JSONWriteData(config, c)
}

// AppAddHandler 添加
func (o *Config) AppAddHandler(c *gin.Context) {
	var p dto.AppCfg
	if err := c.ShouldBind(&p); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	data := &model.Config{}
	data.Name = dto.AppCfgName
	data.Value = util.JString(p)
	data.CreatedBy = middleware.GetUser(c).UserName
	if err := orm.DbCreate(data); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWriteData(nil, c)
}

// AppUpdateHandler 更新
func (o *Config) AppUpdateHandler(c *gin.Context) {
	var p dto.AppCfg
	if err := c.ShouldBind(&p); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	data := &model.Config{}
	data.Value = util.JString(p)
	data.CreatedBy = middleware.GetUser(c).UserName
	if err := orm.DbUpdateModelBy(data, "Name like ?", dto.AppCfgName); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWriteData(nil, c)
}

// OfficialGetHandler 配置
func (o *Config) OfficialGetHandler(c *gin.Context) {
	var (
		data   model.Config
		config officialcfg
	)
	if err := orm.DbFirstBy(data, "Name like ?", dto.OfficialCfgName); err == nil {
		util.StringVal(data.Value, &config)
	}
	ctx.JSONWriteData(gin.H{"data": config}, c)
}

// OfficialAddHandler 添加
func (o *Config) OfficialAddHandler(c *gin.Context) {
	var p officialcfg
	if err := c.ShouldBind(&p); err != nil {
		ctx.JSONWriteError(err, c)
	}
	data := &model.Config{}
	data.Name = dto.OfficialCfgName
	data.Value = util.JString(p)
	data.CreatedBy = middleware.GetUser(c).UserName
	if err := orm.DbCreate(data); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWriteData(nil, c)
}

// OfficialUpdateHandler 更新
func (o *Config) OfficialUpdateHandler(c *gin.Context) {
	var p officialcfg
	if err := c.ShouldBind(&p); err != nil {
		ctx.JSONWriteError(err, c)
	}
	data := &model.Config{}
	data.Value = util.JString(p)
	data.CreatedBy = middleware.GetUser(c).UserName
	if err := orm.DbUpdateModelBy(data, "Name like ?", dto.OfficialCfgName); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWriteData(nil, c)
}

func (o Config) Routers(r *gin.RouterGroup) {
	r.GET("/app", o.AppGetHandler)
	r.POST("/app", o.AppAddHandler)
	r.PUT("/app", o.AppUpdateHandler)

	r.GET("/official", o.OfficialGetHandler)
	r.POST("/official", o.OfficialAddHandler)
	r.PUT("/official", o.OfficialUpdateHandler)
}
